<?php

namespace app\admin\controller;

use think\Db;

class Exam extends Base
{

    /**
     *增加试题
     */
    public function addExam()
    {
        $data = [];
        input('content') ? $data['content'] = input('content') : $this->err('缺少添加内容');
        input('optionsA') ? $data['optionsA'] = input('optionsA') : $this->err('缺少选择选项A');
        input('optionsB') ? $data['optionsB'] = input('optionsB') : $this->err('缺少选择选项B');
        input('optionsC') ? $data['optionsC'] = input('optionsC') : true;
        input('optionsD') ? $data['optionsD'] = input('optionsD') : true;
        input('rightOption') ? $data['rightOption'] = input('rightOption') : $this->err('缺少正确选择选项');
        $data['createTime'] = createTime();
        input('sort') ? $data['sort'] = input('sort') : $this->err('缺少排序值');

        $res = model('exam')->editData($data);
        $res ? $this->suc($res) : $this->err('添加失败');

    }

    /**
     * 修改试题
     */
    public function editExam()
    {
        $data = [];
        input('id') ? $data['id'] = input('id') : $this->err('缺少试题主键');
        input('content') ? $data['content'] = input('content') : $this->err('缺少修改内容');
        input('optionsA') ? $data['optionsA'] = input('optionsA') : $this->err('缺少修改选项A');
        input('optionsB') ? $data['optionsB'] = input('optionsB') : $this->err('缺少修改选项B');
        input('jobId') ? $data['jobId'] = input('jobId') : $this->err('缺少工种编码');
        input('optionsC') ? $data['optionsC'] = input('optionsC') : true;
        input('optionsD') ? $data['optionsD'] = input('optionsD') : true;
        input('rightOption') ? $data['rightOption'] = input('rightOption') : $this->err('缺少修改正确选项');
        input('sort') ? $data['sort'] = input('sort') : $this->err('缺少修改排序值');
        $res = model('exam')->editData($data, $data['id']);
        $res ? $this->suc($res) : $this->err('修改失败');

    }

    /**
     *
     */
    public function getExamList()
    {
        $where = [];
        //   input('id') ? $where['id']=input('id') : $this->$this->err('不能缺少主键');
        input('content') ? $where['e.content'] = ['like', '%' . input('content') . '%'] : true;
        input('jobId') ? $where['e.jobId'] = input('jobId') : true;

        $res = model('exam')
            ->alias('e')
            ->field('e.id, e.content, e.rightOption, e.sort, e.createTime, j.name as jobName')
            ->join('job j', 'j.id=e.jobId', 'left')
            ->where($where)
            ->order('e.id desc')
            ->paginate(10)
            ->toArray();
        $res ? $this->suc($res) : $this->err('获取信息失败');

        $res = model('exam')->getListByPage($where,10,'','sort desc');
        $res ? $this->suc($res) : $this->err('查询失败');
    }

    public function getExamDetail()
    {
        $where = [];
        input('id') ? $where['id'] = input('id') : $this->$this->err('缺少主键');

        $res = model('exam')->where($where)->find();
        $res ? $this->suc($res) : $this->err('查询失败');

    }

    public function delExam()
    {
        $where = [];
        input('id') ? $where['id'] = input('id') : $this->$this->err('缺少主键');

        $res = model('exam')->where($where)->delete();
        $res ? $this->suc($res) : $this->err('删除失败');
    }
}
